package git.soulbgm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import git.soulbgm.config.SysConfig;
import git.soulbgm.pojo.NodeInfo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import git.soulbgm.pojo.ServiceInfo;
import git.soulbgm.service.ServiceInfoService;
import git.soulbgm.mapper.ServiceInfoMapper;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务信息表
 *
 * @author SoulBGM
 * @date 2020-11-18 10:03:05
 */
@Service
public class ServiceInfoServiceImpl extends ServiceImpl<ServiceInfoMapper, ServiceInfo> implements ServiceInfoService {

    @Override
    public Integer getServiceCode() {
        List<Integer> codeList = list(new LambdaQueryWrapper<ServiceInfo>().select(ServiceInfo::getServiceCode)).stream().map(ServiceInfo::getServiceCode).sorted().collect(Collectors.toList());
        int code = 1;
        if (!codeList.isEmpty()) {
            Integer dbMaxCode = codeList.get(codeList.size() - 1);
            code = dbMaxCode + 1;
        }
        return code;
    }

    /**
     * 获得所有缓存服务信息
     * Cacheable
     * value：缓存key的前缀。
     * key：缓存key的后缀。
     * sync：设置如果缓存过期是不是只放一个请求去请求数据库，其他请求阻塞，默认是false。
     *
     * @return {@link List}<{@link ServiceInfo}>
     */
    @Cacheable(value = "serviceInfoAll", sync = true)
    @Override
    public List<ServiceInfo> getCacheServiceInfoAll() {
        return list(new LambdaQueryWrapper<ServiceInfo>().eq(ServiceInfo::getRegionCode, SysConfig.getConfig().getRegionCode()));
    }

    @CacheEvict(value = {"serviceInfoAll"}, allEntries = true)
    @Override
    public void clearCache() {
    }

}